package com.evo.chat.web.service.impl;

import com.evo.chat.admin.domain.Reply;
import com.evo.chat.admin.repository.ReplyRepository;
import com.evo.chat.common.Result;
import com.evo.chat.common.utils.ResultUtil;
import com.evo.chat.web.domain.UserInfo;
import com.evo.chat.web.domain.Words;
import com.evo.chat.web.repository.UserRepository;
import com.evo.chat.web.repository.WordsRepository;
import com.evo.chat.web.service.WordsService;
import com.evo.chat.web.vo.ReplyObjectVo;
import com.evo.chat.web.vo.WordObjectVo;
import com.evo.chat.web.vo.WordsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author luwenzheng
 * date 2020-02-08
 */
@Service
public class WordsServiceImpl implements WordsService {

    @Autowired
    WordsRepository repository;

    @Autowired
    ReplyRepository replyRepository;

    @Autowired
    UserRepository userRepository;

    @Override
    public Result createWord(Words words) {
        System.out.println("words-> " + words.toString());
        repository.save(words);
        return ResultUtil.success();
    }

    @Override
    public Result queryUserWords(Integer userId) {
        List<Words> byUserId = repository.findByUserId(userId);
        System.out.println("ress=> " + byUserId.size());

        System.out.println("Words=> " + byUserId.toString());
        WordsVo wordsVo = convertFromModel(byUserId);
        System.out.println("ress=> " + wordsVo.toString());

        return ResultUtil.success(wordsVo);
    }

    @Override
    public Result delWords(Integer wordId) {
        System.out.println("删除留言-> " + wordId);
        Optional<Words> byId = repository.findById(wordId);
        System.out.println("留言=" + byId.get().toString());
        repository.delete(byId.get());
//        repository.deleteById(wordId);
        return ResultUtil.success();
    }

    @Override
    public Result queryWordsByName(String name) {
        UserInfo byName = userRepository.findByName(name);
        return queryUserWords(byName.getId());
    }

    private WordsVo convertFromModel(List<Words> wordList){
        if(wordList.size() == 0){
            return null;
        }
        List<WordObjectVo> words = new ArrayList<>();
        for (int i = 0; i < wordList.size(); i++) {
            System.out.println("id====> " + wordList.get(i).getId());
            Optional<Words> byId = repository.findById(wordList.get(i).getId());
            List<Reply> byWordsId = replyRepository.findByWords(byId.get());
            List<ReplyObjectVo> replyObjectVos = new ArrayList<>();
            for (int j = 0; j < byWordsId.size(); j++) {
                ReplyObjectVo replyObjectVo = new ReplyObjectVo();
                replyObjectVo.setContent(byWordsId.get(j).getrContent());
                replyObjectVo.setReplyId(byWordsId.get(j).getId());
                replyObjectVos.add(replyObjectVo);
            }
            WordObjectVo wordObjectVo = new WordObjectVo();
            wordObjectVo.setContent(wordList.get(i).getContent());
            wordObjectVo.setUserName(wordList.get(i).getUser().getName());
            wordObjectVo.setWordId(wordList.get(i).getId());
            wordObjectVo.setReplyContent(replyObjectVos);
            words.add(wordObjectVo);
        }
        Collections.reverse(words); // 倒序排列
        WordsVo wordsVo = new WordsVo();
        wordsVo.setWords(words);
        return wordsVo;
    }
}
